﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using XiaoGang.Soft.XGWMS.BLL;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.UI
{
    public partial class M002_USER_GROUP : FormBase
    {
        BLL_M002_USER_GROUP bllUserGroup = new BLL_M002_USER_GROUP();
        private bool isInit = true;
        private const string dataName = "用户分组";

        #region 方法

        public M002_USER_GROUP()
        {
            InitializeComponent();
        }

        /// <summary>
        /// InitialGridView
        /// </summary>
        private void InitialGridView()
        {
            grdMain.AllowUserToAddRows = false;
            grdMain.AllowUserToResizeColumns = true;
            grdMain.AllowUserToResizeRows = false;
            grdMain.ColumnHeadersHeight = 30;
            grdMain.ColumnHeadersVisible = true;
            grdMain.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            grdMain.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.ReadOnly = true;    
            grdMain.RowHeadersVisible = false;
            grdMain.RowTemplate.Resizable = DataGridViewTriState.False;
            grdMain.RowTemplate.Height = 25;
            grdMain.RowTemplate.ReadOnly = true;
            grdMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            grdMain.TabStop = false;      

            grdMain.Columns.Add("NO", "NO.");
            grdMain.Columns.Add("GROUP_ID", "用户分组ID");
            grdMain.Columns.Add("GROUP_NM", "用户分组名称");
            grdMain.Columns.Add("MEMO", "备注");
            DataGridViewButtonColumn btnDelete = new DataGridViewButtonColumn() { Text = "删除", Width = 80, HeaderText = "删除" };
            grdMain.Columns.Add(btnDelete);

            grdMain.Columns["NO"].Width = 40;
            grdMain.Columns["NO"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grdMain.Columns["GROUP_ID"].Width = 150;
            grdMain.Columns["GROUP_NM"].Width = 200;
            grdMain.Columns["MEMO"].Width = 280;            
        }

        /// <summary>
        /// InitialControl
        /// </summary>
        private void InitialControl()
        {
            txtGROUP_ID.Text = "";
            txtGROUP_NM.Text = "";
            txtMEMO.Text = "";
        }

        /// <summary>
        /// BindGridData
        /// </summary>
        private void BindGridData()
        {
            if (isInit)
            {
                return;
            }
            grdMain.Rows.Clear();
            this.Cursor = Cursors.WaitCursor;
            MOD_M002_USER_GROUP modUserGroup = new MOD_M002_USER_GROUP();
            BLL_M002_USER_GROUP bllUserGroup = new BLL_M002_USER_GROUP();
            ReturnValue returnValue;
            modUserGroup.GROUP_ID = this.txtGROUP_ID.Text.Trim();
            modUserGroup.GROUP_NM = this.txtGROUP_NM.Text.Trim();
            modUserGroup.MEMO = this.txtMEMO.Text.Trim();
            try
            {
                returnValue = bllUserGroup.GerUserGroupInfo(modUserGroup);
                if (!returnValue.IsSuccess)
                {
                    Method.MessageShow(returnValue.ErrorID,dataName);
                    this.Cursor = Cursors.Default;
                    return;
                }
                for (int i = 0; i < returnValue.ResultDataSet.Tables[0].Rows.Count; i++)
                {
                    grdMain.Rows.Add((i + 1).ToString(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["GROUP_ID"].ToString(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["GROUP_NM"].ToString(),
                        returnValue.ResultDataSet.Tables[0].Rows[i]["MEMO"].ToString(),
                        "删除");
                }
                groupBox2.Text = String.Format("共{0}条记录", returnValue.ResultDataSet.Tables[0].Rows.Count);
            }
            catch
            {
                Method.MessageShow("E11004",dataName);//An unknown error process!
            }
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// SetControlsIndex
        /// </summary>
        protected void SetControlsIndex()
        {
            int intIndex = 0;intIndex++;
            txtGROUP_ID.TabIndex = intIndex;intIndex++;
            txtGROUP_NM.TabIndex = intIndex;intIndex++;
            txtMEMO.TabIndex = intIndex;intIndex++;
            btnAdd.TabIndex = intIndex;intIndex++;
            btnExit.TabIndex = intIndex;
        }

        /// <summary>
        /// Initial
        /// </summary>
        private void Initial()
        {
            SetControlsIndex();
            InitialGridView();
            InitialControl();
            isInit = false;
        }

        /// <summary>
        /// 用户分组是否被使用
        /// </summary>
        /// <param name="UserGroupID"></param>
        /// <returns></returns>
        private bool IsUserGroupUsed(string UserGroupID)
        {
            ReturnValue returnValue = bllUserGroup.GetUserGroupUsed(UserGroupID);
            if (returnValue.IsSuccess)
            {
                if (returnValue.Count > 0)
                {
                    Method.MessageShow("W11D01", dataName);//本@正在使用!不能删除!
                    return true;
                }
                else
                {
                    return false;   
                }
            }
            else
            {
                Method.MessageShow(returnValue.ErrorID,dataName);
                return true;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M002_USER_GROUP_Load(object sender, EventArgs e)
        {
            Initial();
            BindGridData();
            txtGROUP_ID.Focus();
        }

        /// <summary>
        /// btnExit_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            this.Close();
        }

        /// <summary>
        /// btnAdd_Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            using (M002UserGroupEdit form = new M002UserGroupEdit())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    //刷新Grid数据
                    BindGridData();
                }
            }
        }        

        /// <summary>
        /// 窗体键盘事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void M002_USER_GROUP_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F5:
                    btnAdd_Click(sender, e);
                    break;
                case Keys.Escape:
                    btnExit_Click(sender, e);
                    break;
            }
        }

        /// <summary>
        /// 双击编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (0 == grdMain.Rows.Count || e.RowIndex == -1)
            {
                return;
            }
            string strGroupID = grdMain.CurrentRow.Cells["GROUP_ID"].Value.ToString();
            string strGroupName = grdMain.CurrentRow.Cells["GROUP_NM"].Value.ToString();
            string strGroupMemo = grdMain.CurrentRow.Cells["MEMO"].Value.ToString();
            if (new M002UserGroupEdit(strGroupID,strGroupName,strGroupMemo).ShowDialog() == DialogResult.OK)
            {
                //刷新Grid数据
                BindGridData();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int intColumnIndex = e.ColumnIndex;
            if (intColumnIndex == 4 && e.RowIndex != -1)
            {
                if (this.Cursor == Cursors.WaitCursor)
                {
                    return;
                }
                this.Cursor = Cursors.WaitCursor;
                //检查分类是否被使用
                if (IsUserGroupUsed(grdMain.CurrentRow.Cells["GROUP_ID"].Value.ToString()))
                {
                    this.Cursor = Cursors.Default;
                    return;
                }

                if (Method.MessageShow("Q22007", dataName) == DialogResult.Yes)//确定要删除该条数据吗?
                {
                    try
                    {
                        ReturnValue returnValue = bllUserGroup.PhysicalDelUserGroup(grdMain.CurrentRow.Cells["GROUP_ID"].Value.ToString());
                        if (!returnValue.IsSuccess)
                        {
                            Method.MessageShow(returnValue.ErrorID, dataName);
                            this.Cursor = Cursors.Default;
                            return;
                        }
                        BindGridData();
                    }
                    catch
                    {
                        Method.MessageShow("E11004",dataName);//An unknown error process!
                    }
                }
                this.Cursor = Cursors.Default;
            }
        }

        private void QueryChanged(object sender, EventArgs e)
        {
            if (this.Cursor == Cursors.WaitCursor)
            {
                return;
            }
            BindGridData();
        }

        #endregion
    }
}